home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Sound / MusicIn / common.h < prev    next >
C/C++ Source or Header  |  1997-09-23  |  17KB  |  476 lines

  1. /**********************************************************************
  2. Copyright (c) 1991 MPEG/audio software simulation group, All Rights Reserved
  3. common.h
  4. **********************************************************************/
  5. /**********************************************************************
  6.  * MPEG/audio coding/decoding software, work in progress              *
  7.  *   NOT for public distribution until verified and approved by the   *
  8.  *   MPEG/audio committee.  For further information, please contact   *
  9.  *   Davis Pan, 508-493-2241, e-mail: pan@gauss.enet.dec.com          *
  10.  *                                                                    *
  11.  * VERSION 4.0                                                        *
  12.  *   changes made since last update:                                  *
  13.  *   date   programmers         comment                               *
  14.  * 2/25/91  Doulas Wong,        start of version 1.0 records          *
  15.  *          Davis Pan                                                 *
  16.  * 5/10/91  W. Joseph Carter    Reorganized & renamed all ".h" files  *
  17.  *                              into "common.h" and "encoder.h".      *
  18.  *                              Ported to Macintosh and Unix.         *
  19.  *                              Added additional type definitions for *
  20.  *                              AIFF, double/SANE and "bitstream.c".  *
  21.  *                              Added function prototypes for more    *
  22.  *                              rigorous type checking.               *
  23.  * 27jun91  dpwe (Aware)        Added "alloc_*" defs & prototypes     *
  24.  *                              Defined new struct 'frame_params'.    *
  25.  *                              Changed info.stereo to info.mode_ext  *
  26.  *                              #define constants for mode types      *
  27.  *                              Prototype arguments if PROTO_ARGS     *
  28.  * 5/28/91  Earle Jennings      added MS_DOS definition               *
  29.  *                              MsDos function prototype declarations *
  30.  * 7/10/91  Earle Jennings      added FLOAT definition as double      *
  31.  *10/ 3/91  Don H. Lee          implemented CRC-16 error protection   *
  32.  * 2/11/92  W. Joseph Carter    Ported new code to Macintosh.  Most   *
  33.  *                              important fixes involved changing     *
  34.  *                              16-bit ints to long or unsigned in    *
  35.  *                              bit alloc routines for quant of 65535 *
  36.  *                              and passing proper function args.     *
  37.  *                              Removed "Other Joint Stereo" option   *
  38.  *                              and made bitrate be total channel     *
  39.  *                              bitrate, irrespective of the mode.    *
  40.  *                              Fixed many small bugs & reorganized.  *
  41.  *                              Modified some function prototypes.    *
  42.  *                              Changed BUFFER_SIZE back to 4096.     *
  43.  * 7/27/92  Michael Li          (re-)Ported to MS-DOS                 *
  44.  * 7/27/92  Masahiro Iwadare    Ported to Convex                      *
  45.  * 8/07/92  mc@tv.tek.com                                             *
  46.  * 8/10/92  Amit Gulati         Ported to the AIX Platform (RS6000)   *
  47.  *                              AIFF string constants redefined       *
  48.  * 8/27/93 Seymour Shlien,      Fixes in Unix and MSDOS ports,        *
  49.  *         Daniel Lauzon, and                                         *
  50.  *         Bill Truerniet                                             *
  51.  **********************************************************************/
  52.  
  53. /***********************************************************************
  54. *
  55. *  Global Conditional Compile Switches
  56. *
  57. ***********************************************************************/
  58.  
  59. /* #define      DEBUG     */ /* ST 10/04/1995 */
  60. #define INT    short         /* ST 10/04/1995 for decode_int.c */
  61.  
  62. #define      UNIX            /* Unix conditional compile switch */
  63. /* #define      MACINTOSH */      /* Macintosh conditional compile switch */
  64. /* #define      MS_DOS    */      /* IBM PC conditional compile switch */
  65. /* #define      MSC60     */      /* Compiled for MS_DOS with MSC v6.0 */
  66. /* #define      AIX       */      /* AIX conditional compile switch    */
  67. /* #define      CONVEX    */      /* CONVEX conditional compile switch */
  68.  
  69. #if defined(MSC60)
  70. #ifndef MS_DOS
  71. #define MS_DOS
  72. #endif
  73. #ifndef PROTO_ARGS
  74. #define PROTO_ARGS
  75. #endif
  76. #endif
  77.  
  78. #ifdef  UNIX
  79. #define         TABLES_PATH     "tables"  /* to find data files */
  80. /* name of environment variable holding path of table files */
  81. #define         MPEGTABENV      "MPEGTABLES"
  82. #define         PATH_SEPARATOR  "/"        /* how to build paths */
  83. #endif  /* UNIX */
  84.  
  85. #ifdef  MACINTOSH
  86. /* #define      TABLES_PATH ":tables:"  */ /* where to find data files */
  87. #endif  /* MACINTOSH */
  88.  
  89. /*
  90.  * Don't define FAR to far unless you're willing to clean up the
  91.  * prototypes
  92.  */
  93. #define FAR /*far*/
  94.  
  95. #ifdef __STDC__
  96. #ifndef PROTO_ARGS
  97. #define PROTO_ARGS
  98. #endif
  99. #endif
  100.  
  101. #ifdef CONVEX
  102. #define SEEK_SET        0
  103. #define SEEK_CUR        1
  104. #define SEEK_END        2
  105. #endif
  106.  
  107. /* MS_DOS and VMS do not define TABLES_PATH, so OpenTableFile will default
  108.    to finding the data files in the default directory */
  109.  
  110. /***********************************************************************
  111. *
  112. *  Global Include Files
  113. *
  114. ***********************************************************************/
  115.  
  116. #include        <stdio.h>
  117. #include        <string.h>
  118. #include        <math.h>
  119.  
  120. #ifdef  UNIX
  121. #include        <unistd.h>
  122. #endif  /* UNIX */
  123.  
  124. #ifdef  MACINTOSH
  125. #include        <stdlib.h>
  126. #include        <console.h>
  127. #endif  /* MACINTOSH */
  128.  
  129. #ifdef  MS_DOS
  130. #include        <stdlib.h>
  131. #ifdef MSC60
  132. #include        <memory.h>
  133. #else
  134. #include        <alloc.h>
  135. #include        <mem.h>
  136. #endif  /* MSC60 */
  137. #endif  /* MS_DOS */
  138.  
  139. #ifdef AMIGA
  140. #include        <stdlib.h>
  141. /* #ifdef M68881 */
  142. #include <m68881.h>
  143. /* #endif */
  144. #endif
  145.  
  146. /***********************************************************************
  147. *
  148. *  Global Definitions
  149. *
  150. ***********************************************************************/
  151.  
  152. /* General Definitions */
  153.  
  154. #ifdef  MS_DOS
  155. #define         FLOAT                   double
  156. #else
  157. #define         FLOAT                   float
  158. #endif
  159.  
  160. #define         FALSE                   0
  161. #define         TRUE                    1
  162. #define         NULL_CHAR               '\0'
  163.  
  164. #define         MAX_U_32_NUM            0xFFFFFFFF
  165. #ifndef PI
  166. #define         PI                      3.14159265358979
  167. #endif
  168. #define         PI4                     PI/4
  169. #define         PI64                    PI/64
  170. #define         LN_TO_LOG10             0.2302585093
  171.  
  172. #define         VOL_REF_NUM             0
  173. #define         MPEG_AUDIO_ID           1
  174. #define         MAC_WINDOW_SIZE         24
  175.  
  176. #define         MONO                    1
  177. #define         STEREO                  2
  178. #define         BITS_IN_A_BYTE          8
  179. #define         WORD                    16
  180. #define         MAX_NAME_SIZE           81
  181. #define         SBLIMIT                 32
  182. #define         FFT_SIZE                1024
  183. #define         HAN_SIZE                512
  184. #define         SCALE_BLOCK             12
  185. #define         SCALE_RANGE             64
  186. #define         SCALE                   32768
  187. #define         CRC16_POLYNOMIAL        0x8005
  188.  
  189. /* MPEG Header Definitions - Mode Values */
  190.  
  191. #define         MPG_MD_STEREO           0
  192. #define         MPG_MD_JOINT_STEREO     1
  193. #define         MPG_MD_DUAL_CHANNEL     2
  194. #define         MPG_MD_MONO             3
  195.  
  196. /* AIFF Definitions */
  197.  
  198. /*
  199.  * Note:  The value of a multi-character constant
  200.  *        is implementation-defined.
  201.  */
  202. #define MAKE_ID(a,b,c,d) ((a<<24)|(b<<16)|(c<<8)|(d))
  203.  
  204. #define         IFF_LONG
  205. #define         IFF_ID_FORM             MAKE_ID( 'F','O','R','M' )
  206. #define         IFF_ID_AIFF             MAKE_ID( 'A','I','F','F' )
  207. #define         IFF_ID_COMM             MAKE_ID( 'C','O','M','M' )
  208. #define         IFF_ID_SSND             MAKE_ID( 'S','S','N','D' )
  209. #define         IFF_ID_MPEG             MAKE_ID( 'M','P','E','G' )
  210.  
  211. /* "bit_stream.h" Definitions */
  212.  
  213. #define         MINIMUM         4    /* Minimum size of the buffer in bytes */
  214. #define         MAX_LENGTH      32   /* Maximum length of word written or
  215.                                         read from bit stream */
  216. #define         READ_MODE       0
  217. #define         WRITE_MODE      1
  218. #define         ALIGNING        8
  219. #define         BINARY          0
  220. #define         ASCII           1
  221. #define         BS_FORMAT       BINARY /* BINARY or ASCII = 2x bytes */
  222. #define         BUFFER_SIZE     16384
  223.  
  224. #define         MIN(A, B)       ((A) < (B) ? (A) : (B))
  225. #define         MAX(A, B)       ((A) > (B) ? (A) : (B))
  226.  
  227. /***********************************************************************
  228. *
  229. *  Global Type Definitions
  230. *
  231. ***********************************************************************/
  232.  
  233. /* Structure for Reading Layer II Allocation Tables from File */
  234.  
  235. typedef struct {
  236.     unsigned short steps;
  237.     unsigned short bits;
  238.     unsigned short group;
  239.     unsigned short quant;
  240. } sb_alloc, *alloc_ptr;
  241.  
  242. typedef sb_alloc        al_table[SBLIMIT][16];
  243.  
  244. /* Header Information Structure */
  245.  
  246. typedef struct {
  247.     int version;
  248.     int lay;
  249.     int error_protection;
  250.     int bitrate_index;
  251.     int sampling_frequency;
  252.     int padding;
  253.     int extension;
  254.     int mode;
  255.     int mode_ext;
  256.     int copyright;
  257.     int original;
  258.     int emphasis;
  259. } layer, *the_layer;
  260.  
  261. /* Parent Structure Interpreting some Frame Parameters in Header */
  262.  
  263. typedef struct {
  264.     layer       *header;        /* raw header information */
  265.     int         actual_mode;    /* when writing IS, may forget if 0 chs */
  266.     al_table    *alloc;         /* bit allocation table read in */
  267.     int         tab_num;        /* number of table as loaded */
  268.     int         stereo;         /* 1 for mono, 2 for stereo */
  269.     int         jsbound;        /* first band of joint stereo coding */
  270.     int         sblimit;        /* total number of sub bands */
  271. } frame_params;
  272.  
  273. /* Double and SANE Floating Point Type Definitions */
  274.  
  275. typedef struct  IEEE_DBL_struct {
  276.     unsigned long   hi;
  277.     unsigned long   lo;
  278. } IEEE_DBL;
  279.  
  280. typedef struct  SANE_EXT_struct {
  281.     unsigned long   l1;
  282.     unsigned long   l2;
  283.     unsigned short  s1;
  284. } SANE_EXT;
  285.  
  286. /* AIFF Type Definitions */
  287.  
  288. typedef char    ID[4];
  289.  
  290. typedef struct  ChunkHeader_struct {
  291.     ID      ckID;
  292.     long    ckSize;
  293. } ChunkHeader;
  294.  
  295. typedef struct  Chunk_struct {
  296.     ID      ckID;
  297.     long    ckSize;
  298.     ID      formType;
  299. } Chunk;
  300.  
  301. typedef struct  CommonChunk_struct {
  302.     ID              ckID;
  303.     long            ckSize;
  304.     short           numChannels;
  305.     unsigned long   numSampleFrames;
  306.     short           sampleSize;
  307.     char            sampleRate[10];
  308. } CommonChunk;
  309.  
  310. typedef struct  SoundDataChunk_struct {
  311.     ID              ckID;
  312.     long            ckSize;
  313.     unsigned long   offset;
  314.     unsigned long   blockSize;
  315. } SoundDataChunk;
  316.  
  317. typedef struct  blockAlign_struct {
  318.     unsigned long   offset;
  319.     unsigned long   blockSize;
  320. } blockAlign;
  321.  
  322. typedef struct  IFF_AIFF_struct {
  323.     short           numChannels;
  324.     unsigned long   numSampleFrames;
  325.     short           sampleSize;
  326.     double          sampleRate;
  327.     unsigned long   sampleType;
  328.     blockAlign      blkAlgn;
  329. } IFF_AIFF;
  330.  
  331. /* "bit_stream.h" Type Definitions */
  332.  
  333. typedef struct  bit_stream_struc {
  334.     FILE        *pt;            /* pointer to bit stream device */
  335.     unsigned char *buf;         /* bit stream buffer */
  336.     int         buf_size;       /* size of buffer (in number of bytes) */
  337.     long        totbit;         /* bit counter of bit stream */
  338.     int         buf_byte_idx;   /* pointer to top byte in buffer */
  339.     int         buf_bit_idx;    /* pointer to top bit of top byte in buffer */
  340.     int         mode;           /* bit stream open in read or write mode */
  341.     int         eob;            /* end of buffer index */
  342.     int         eobs;           /* end of bit stream flag */
  343.     char        format;
  344.  
  345.     /* format of file in rd mode (BINARY/ASCII) */
  346. } Bit_stream_struc;
  347.  
  348. /***********************************************************************
  349. *
  350. *  Global Variable External Declarations
  351. *
  352. ***********************************************************************/
  353.  
  354. extern char     *mode_names[4];
  355. extern char     *layer_names[3];
  356. extern double   s_freq[4];
  357. extern int      bitrate[3][15];
  358. extern double FAR multiple[64];
  359.  
  360. /***********************************************************************
  361. *
  362. *  Global Function Prototype Declarations
  363. *
  364. ***********************************************************************/
  365.  
  366. /* The following functions are in the file "common.c" */
  367.  
  368. #ifdef  PROTO_ARGS
  369. extern FILE           *OpenTableFile(char*);
  370. extern int            read_bit_alloc(int, al_table*);
  371. extern int            pick_table(frame_params*);
  372. extern int            js_bound(int, int);
  373. extern void           hdr_to_frps(frame_params*);
  374. extern void           WriteHdr(frame_params*, FILE*);
  375. extern void           WriteBitAlloc(unsigned int[2][SBLIMIT], frame_params*,
  376.                         FILE*);
  377. extern void           WriteScale(unsigned int[2][SBLIMIT],
  378.                         unsigned int[2][SBLIMIT], unsigned int[2][3][SBLIMIT],
  379.                         frame_params*, FILE*);
  380. extern void           WriteSamples(int, unsigned int FAR [SBLIMIT],
  381.                         unsigned int[SBLIMIT], frame_params*, FILE*);
  382. extern int            NumericQ(char*);
  383. extern int            BitrateIndex(int, int);
  384. extern int            SmpFrqIndex(long);
  385. extern int            memcheck(char*, int, int);
  386. extern void           FAR *mem_alloc(unsigned long, char*);
  387. extern void           mem_free(void**);
  388. extern void           double_to_extended(double*, char[10]);
  389. extern void           extended_to_double(char[10], double*);
  390. extern long           aiff_read_headers(FILE*, IFF_AIFF*);
  391. extern int            aiff_seek_to_sound_data(FILE*);
  392. extern int            aiff_write_headers(FILE*, IFF_AIFF*);
  393. extern int            refill_buffer(Bit_stream_struc*);
  394. extern void           empty_buffer(Bit_stream_struc*, int);
  395. extern void           open_bit_stream_w(Bit_stream_struc*, char*, int);
  396. extern void           open_bit_stream_r(Bit_stream_struc*, char*, int);
  397. extern void           close_bit_stream_r(Bit_stream_struc*);
  398. extern void           close_bit_stream_w(Bit_stream_struc*);
  399. extern void           alloc_buffer(Bit_stream_struc*, int);
  400. extern void           desalloc_buffer(Bit_stream_struc*);
  401. extern void           back_track_buffer(Bit_stream_struc*, int);
  402. extern unsigned int   get1bit(Bit_stream_struc*);
  403. extern void           put1bit(Bit_stream_struc*, int);
  404. extern unsigned long  look_ahead(Bit_stream_struc*, int);
  405. extern unsigned long  getbits(Bit_stream_struc*, int);
  406. extern void           putbits(Bit_stream_struc*, unsigned int, int);
  407. extern void           byte_ali_putbits(Bit_stream_struc*, unsigned int, int);
  408. extern unsigned long  byte_ali_getbits(Bit_stream_struc*, int);
  409. extern unsigned long  sstell(Bit_stream_struc*);
  410. extern int            end_bs(Bit_stream_struc*);
  411. extern int            seek_sync(Bit_stream_struc*, long, int);
  412. extern void           I_CRC_calc(frame_params*, unsigned int[2][SBLIMIT],
  413.                         unsigned int*);
  414. extern void           II_CRC_calc(frame_params*, unsigned int[2][SBLIMIT],
  415.                         unsigned int[2][SBLIMIT], unsigned int*);
  416. extern void           update_CRC(unsigned int, unsigned int, unsigned int*);
  417. extern void           read_absthr(FLOAT*, int);
  418.  
  419. #ifdef  MACINTOSH
  420. extern void           set_mac_file_attr(char[MAX_NAME_SIZE], short, OsType,
  421.                         OsType);
  422. #endif
  423. #ifdef MS_DOS
  424. extern char           *new_ext(char *filename, char *extname);
  425. #endif
  426.  
  427. #else
  428. extern FILE           *OpenTableFile();
  429. extern int            read_bit_alloc();
  430. extern int            pick_table();
  431. extern int            js_bound();
  432. extern void           hdr_to_frps();
  433. extern void           WriteHdr();
  434. extern void           WriteBitAlloc();
  435. extern void           WriteScale();
  436. extern void           WriteSamples();
  437. extern int            NumericQ();
  438. extern int            BitrateIndex();
  439. extern int            SmpFrqIndex();
  440. extern int            memcheck();
  441. extern void           FAR *mem_alloc();
  442. extern void           mem_free();
  443. extern void           double_to_extended();
  444. extern void           extended_to_double();
  445. extern long           aiff_read_headers();
  446. extern int            aiff_seek_to_sound_data();
  447. extern int            aiff_write_headers();
  448. extern int            refill_buffer();
  449. extern void           empty_buffer();
  450. extern void           open_bit_stream_w();
  451. extern void           open_bit_stream_r();
  452. extern void           close_bit_stream_r();
  453. extern void           close_bit_stream_w();
  454. extern void           alloc_buffer();
  455. extern void           desalloc_buffer();
  456. extern void           back_track_buffer();
  457. extern unsigned int   get1bit();
  458. extern void           put1bit();
  459. extern unsigned long  look_ahead();
  460. extern unsigned long  getbits();
  461. extern void           putbits();
  462. extern void           byte_ali_putbits();
  463. extern unsigned long  byte_ali_getbits();
  464. extern unsigned long  sstell();
  465. extern int            end_bs();
  466. extern int            seek_sync();
  467. extern void           I_CRC_calc();
  468. extern void           II_CRC_calc();
  469. extern void           update_CRC();
  470. extern void           read_absthr();
  471.  
  472. #ifdef MS_DOS
  473. extern char           *new_ext();
  474. #endif
  475. #endif
  476.